package com.wenwei.paper.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


import com.jaeger.library.StatusBarUtil;
import com.orhanobut.logger.Logger;
import com.wenwei.paper.R;
import com.wenwei.paper.base.BaseActivity;
import com.wenwei.paper.base.Constant;
import com.wenwei.paper.bean.UserLoginBean;
import com.wenwei.paper.utils.RogerSPUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bingoogolapple.bgabanner.BGABanner;
import cn.bingoogolapple.bgabanner.BGABannerUtil;

/**
 * Created by Administrator on 2016/8/9.
 */
public class GuideActivity extends BaseActivity {
    @Bind(R.id.banner_guide_content)
    BGABanner bannerGuideContent;
    @Bind(R.id.tv_guide_skip)
    TextView tvGuideSkip;
    @Bind(R.id.btn_guide_enter)
    Button btnGuideEnter;
    private List<View> views;
    private Context mContext;

    @Override
    protected void init() {
        super.init();
        //第一次安装保存配置信息
        SharedPreferences sharedPreferences = getSharedPreferences("configuration", 0);
        SharedPreferences.Editor  editor  =  sharedPreferences.edit();
        editor.putString("is_first","n");
        editor.commit();
    }

    @Override
    protected void initView() {
        mContext = this;
        StatusBarUtil.setTranslucent(this, 30);
        views = new ArrayList<>();
        views.add(BGABannerUtil.getItemImageView(this, R.drawable.splash1));
        views.add(BGABannerUtil.getItemImageView(this, R.drawable.splash2));
        views.add(BGABannerUtil.getItemImageView(this, R.drawable.splash3));
        bannerGuideContent.setDelegate(new BGABanner.Delegate() {
            @Override
            public void onBannerItemClick(BGABanner banner, View itemView, Object model, int position) {
                Logger.d("onBannerItemClick");
                if (position == (views.size()-1)){
                    goActivity();
                }
            }
        });
        bannerGuideContent.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Logger.d("onPageScrolled");
            }

            @Override
            public void onPageSelected(int position) {
                Logger.d("onPageSelected");
//                if (position == (views.size()-1)){
//                    goActivity();
//                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                Logger.d("onPageScrollStateChanged");
            }
        });
//        bannerGuideContent.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//                if (position == bannerGuideContent.getItemCount() - 2) {
//                    ViewCompat.setAlpha(btnGuideEnter, positionOffset);
//                    ViewCompat.setAlpha(tvGuideSkip, 1.0f - positionOffset);
//
//                    if (positionOffset > 0.5f) {
//                        btnGuideEnter.setVisibility(View.GONE);
//                        tvGuideSkip.setVisibility(View.GONE);
//                    } else {
//                        btnGuideEnter.setVisibility(View.GONE);
//                        tvGuideSkip.setVisibility(View.GONE);
//                    }
//                } else if (position == bannerGuideContent.getItemCount() - 1) {
//                    tvGuideSkip.setVisibility(View.GONE);
//                    btnGuideEnter.setVisibility(View.VISIBLE);
//                    ViewCompat.setAlpha(btnGuideEnter, 1.0f);
//                    bannerGuideContent.setIsNeedShowIndicatorOnOnlyOnePage(false);
//                } else {
//                    tvGuideSkip.setVisibility(View.GONE);
//                    ViewCompat.setAlpha(tvGuideSkip, 1.0f);
//                    btnGuideEnter.setVisibility(View.GONE);
//                    bannerGuideContent.setIsNeedShowIndicatorOnOnlyOnePage(true);
//                }
//            }
//        });
    }

    @Override
    protected void initData() {
        super.initData();
        bannerGuideContent.setOverScrollMode(View.OVER_SCROLL_NEVER);
        bannerGuideContent.setData(views);
    }

    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_guide;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_guide_skip, R.id.btn_guide_enter})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_guide_skip:
                goActivity();
                break;
            case R.id.btn_guide_enter:
                goActivity();
                break;
        }
    }

    private void goActivity(){
        UserLoginBean ub = RogerSPUtils.getObject(mContext, Constant.SP_USER_LOGIN_RESULT,UserLoginBean.class);
        if(ub == null){
            goActivity(mContext,LoginActivity.class);
        }else {
            goActivity(mContext,HostActivity.class);
        }
        finish();
    }
}
